<?php
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require_once('../utils/Pager.php');
require("../utils/Session.php");

$commonUtils = new CommonUtils();
$baseDAO = new BaseDAO();
$onlineUser = Session::get('fullname');
if($onlineUser == null){
	$commonUtils->redirect("../login.php"); 
}

$path = "img/uploads/thumbs";
$dir = $root_path . $path;

$action = $commonUtils->getParameter("action");

if(isset($action) && $action == 'delete'){
	$checkList = $commonUtils->getParameter("checkList");
	if (isset($checkList) && is_array($checkList)) {
		foreach($checkList as $filename){
			$commonUtils->delfile($filename, $dir);
			$commonUtils->delfile($filename, $root_path . 'img/uploads');
		}	
	}
}
$files = $commonUtils->readImagesFile($dir);
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=100" >
		<title>NguyenDau - Quản lý hình ảnh</title>
		<link rel="stylesheet" type="text/css" href="../css/diary.css"/>
		<link rel="stylesheet" type="text/css" href="../css/jquery.alert.css"/>
		<script src="../js/jquery-1.7.2.min.js"></script>
		<script src="../js/jquery.alerts.js"></script>
		<script src="../js/global.js"></script>
	</head>
	<body>
		<?php include '../include/header.php';?>
		<center>
			<div class="content">
				<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="formImageManagement" id="formImageManagement" >
					<div class="tableHeader">
						&nbsp;Danh sách hình ảnh (<span style="color:yellow;"><?php echo count($files). ' hình'; ?></span>)
					</div>
					<div>
						<?php foreach ($files as $imgFile){?>
						<div style="width:100px;float:left;margin:2px;border:solid 1px #ccc;padding:2px;">
							<div><img src="<?php echo '../' . $path. '/' .$imgFile;?>" style="max-width:100px;"/></div>
							<div style="text-align:center"><input type="checkbox" name="checkList[]" value="<?php echo $imgFile; ?>"/></div>
						</div>
						<?php }?>
						<div style="clear:both;"></div>
					</div>
					<div><?php ?></div>
					<div class="btArea">
						<input type="button" value="Xóa" id="btDelete" />
						<input type="hidden" name="action" id="crudaction">
					</div>
					<script type="text/javascript">
					$(function(){
						$("#btDelete").click(function(){
							if($('input[name="checkList[]"]:checked').length > 0){
								if(confirm('Bạn có chắc chắn khi xóa những phần tử đã chọn?')){
									$("#crudaction").val("delete");
									$("#formImageManagement").submit();
								}
							}else{
								alert('Bạn phải chọn ít nhất một phần tử');
							}
						});
					});
					</script>	
				</form>
			</div>
		</center>
		<?php include '../include/footer.php';?>
	</body>
</html>
